package com.zsh.wanandroid.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zsh.wanandroid.R;
import com.zsh.wanandroid.bean.RankResult;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zsh
 * @date 2020/3/13.
 * 功能：排名页面adapter
 */

public class RankAdapter extends RecyclerView.Adapter<RankAdapter.ViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<RankResult.Data.Datas> rankList = new ArrayList<>();

    public RankAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<RankResult.Data.Datas> list) {
        rankList.addAll(list);
        notifyDataSetChanged();
    }

    public void addData(List<RankResult.Data.Datas> list) {
        rankList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData() {
        rankList.clear();
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.rank_item_recycleview, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ViewHolder viewHolder = holder;
        viewHolder.mTvRank.setText(rankList.get(position).getRank()+"");
        viewHolder.mTvUsername.setText(rankList.get(position).getUsername());
        viewHolder.mTvCoinCount.setText(rankList.get(position).getCoinCount()+"");
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return rankList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView mTvRank, mTvUsername, mTvCoinCount;

        public ViewHolder(View itemView) {
            super(itemView);
            mTvRank = (TextView) itemView.findViewById(R.id.tvRank);
            mTvUsername = (TextView) itemView.findViewById(R.id.tvUsername);
            mTvCoinCount = (TextView) itemView.findViewById(R.id.tvCoinCount);
        }
    }

}
